package com.arm.controller.admin.order;

import com.arm.controller.BaseController;
import com.arm.def.Constant;
import com.arm.module.Order;
import com.arm.module.OrderItem;
import com.arm.module.Payment;
import com.arm.service.OrderItemService;
import com.arm.service.OrderService;
import com.arm.service.PaymentService;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/admin/order")
public class AdminOrderController extends BaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private PaymentService paymentService;


    /**
     * 查询订单列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getOrderList")
    public ModelAndView getOrderList(Long orderId, Byte status, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("admin/order/getOrderList");
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
             pageSize = Constant.PAGESIZENum;

        }
        Map<String, String> map = new HashMap<>();
        if (orderId != null) {
            map.put("orderId", String.valueOf(orderId));
        }
        if (status != null) {
            map.put("status", String.valueOf(status));
        }

        PageHelper.startPage(page, pageSize);
        List<Order> orderList = orderService.getOrderList(map);
        PageInfo<Order> pageinfo = new PageInfo<>(orderList);

        logger.info(pageinfo.toString());
        m.addObject("pageInfo", pageinfo);
        m.addObject("orderId", orderId);
        m.addObject("status", status);
        return m;
    }
    /**
     * 添加物流
     * @param orderId
     * @return
     */
   /* @RequestMapping("/logistics")
    public ModelAndView logistics(Long orderId) {
        ModelAndView m = new ModelAndView("/admin/order/logistics");

      //OrderItem orderItem=orderItemService.getOrderItemByOrderItemId(orderId);
      Order order = orderService.getOrderById(orderId);

     Map<String,String> map=new HashMap<>();
     map.put("orderId",String.valueOf(orderId));
     ExpressInfo expressInfo=expressInfoService.selectExpressInfo(map);
     m.addObject("order",order);
     m.addObject("expressInfo",expressInfo);
     return m;
    }*//**/


    /**
     * 查询订单产品详情
     *
     * @param request
     * @return
     */
    @RequestMapping("/getOrderItemList")
    public ModelAndView getOrderItemList(HttpServletRequest request, Integer page, Integer pageSize, Long orderId) {
        ModelAndView m = new ModelAndView("admin/order/getOrderItemList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        map.put("orderId", String.valueOf(orderId));
        PageHelper.startPage(page, pageSize);
        List<OrderItem> orderItemList = orderItemService.selectOrderItemList(map);
        PageInfo<OrderItem> pageinfo = new PageInfo<>(orderItemList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean", map);
        return m;
    }

    /**
     * 查询订单支付详情
     *
     * @param request
     * @return
     */
    @RequestMapping("/getPayMentList")
    public ModelAndView getPayMentList(HttpServletRequest request, Long orderId) {
        ModelAndView m = new ModelAndView("admin/order/getPayMentList");
        Map<String, String> map = this.showRequestParameters(request);
       /* if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));*/
        map.put("orderId", String.valueOf(orderId));
        List<Payment> payMentList = paymentService.selectpaymentListByOrderId(map);
        PageInfo<Payment> pageinfo = new PageInfo<>(payMentList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean", map);
        return m;
    }

    /**
     * @param orderId
     * @return
     */
    @PostMapping(value = "/modifyOrderStatus")
    public void modifyOrderItemStatus(Long orderId, Byte status) {
        ResultObj resultObj = new ResultObj();
        try {
            orderService.modifyOrderStatus(orderId, status);
            resultObj.setMsg("操作成功");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        logger.info("resultObj====>>>>" + resultObj.toJson());
    }


}
